from configparser import ConfigParser

from Excel2XmlTask import Excel2XmlTask
from openpyxl import load_workbook
import os
if __name__ == "__main__":

    # 获取新文档中的 key，如果这些 key 在旧文档中出现则忽略这些旧文档中的字段
    def getNewKey(excel_abs_path, sheet_name,
                  row_count,
                  column_index,
                  new_key_should_skip: []):
        book = load_workbook(filename=excel_abs_path, read_only=True)
        sheet = book.get_sheet_by_name(sheet_name)
        for rowIndex in range(row_count):
            if rowIndex <= 1:
                continue
            new_key_should_skip.append(sheet.cell(rowIndex, column_index).value )


    home_dir = "/Users/h"
    taskList = []
    new_key_should_skip = []
    cfg = ConfigParser()
    cfg.read('excel2xml.ini', encoding='utf-8')
    cfg.sections()
    file_array = cfg.get('production', "file_array")
    type_array = cfg.get('production', "type_array").split(",")
    target_file_dir = cfg.get('production', "target_file_dir")
    key_column_index = int(cfg.get('production', "key_column_index"))
    value_column_index_array = cfg.get('production', "value_column_index").split(",")
    sheet_name = cfg.get('production', "sheet_name")
    last_line = int(cfg.get('production', "last_line"))
    print(file_array)

    new_key_file_abs_path = cfg.get('production', "new_key_file_abs_path")
    new_key_sheet_name = cfg.get('production', "new_key_sheet_name")
    new_key_row_count = int(cfg.get('production', "new_key_row_count"))
    new_key_column_index = int(cfg.get('production', "new_key_column_index"))
    if new_key_file_abs_path !=''and new_key_file_abs_path!=None :



         getNewKey(new_key_file_abs_path,
              new_key_sheet_name,
              new_key_row_count,
              new_key_column_index,new_key_should_skip)
    print( new_key_should_skip)

    for i in range(len(type_array)):
        file = file_array
        langtype = type_array[i]
        value_column_index = (int)(value_column_index_array[i])
        target_file = None
        if 'en' == langtype:
            target_file =os.path.join( target_file_dir,  "values","strings.xml")
        else:
            target_file =os.path.join( target_file_dir ,"".join(["values-" , langtype] ), "strings.xml")
        file_type = None
        if file.endswith('.xlsx'):
            file_type = "xlsx"
        else:
            file_type = "xls"
        taskList.append(
            Excel2XmlTask(
                file,
                sheet_name,
                target_file,
                file_type,
                last_line,
                new_key_should_skip,
                key_column_index,
                value_column_index,
            )
        )

    for task in taskList:
        print()
        print()
        print()
        print()
        task.run()
        print()
        print()
        print()
        print()
